package com.tank.util;

import java.net.URL;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.tank.bean.MailValid;
import com.tank.bean.User;

public class MailUtil {

	public static void registerUser(User user, MailValid mv) throws Exception {
		HtmlEmail m = new HtmlEmail();
		m.setHostName("smtp.163.com");
		m.setAuthentication("tankswap", "502970141");
		m.addTo(user.getMail(), user.getName());
		m.setFrom("tankswap@163.com", "tankswap");
		m.setSubject("注册成功，请验证");
		m.setCharset("UTF-8");
		
		String validuri="http://localhost:8080/valid?mv.id="+mv.getId()+"&mv.action="+mv.getUuid();//验证操作入口
		
		m.setHtmlMsg("<html> <head> <title>  </title>  </head> <body> " +
				"<a href='"+validuri+"'>验证地址："+validuri+"</a> " +
				"</body> </html>");
		m.setTextMsg("not support html setCharset");
		m.send();
		System.out.println("sended!");

	}
	
	public static void main(String[] args) {
		
		User u=new User();
		u.setName("zhuoyr");
		u.setMail("zhuogenggai@qq.com");
		
		try {
			registerUser(u,new MailValid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void resetValid(User user, MailValid mv) throws EmailException {
		
		HtmlEmail m = new HtmlEmail();
		m.setHostName("smtp.163.com");
		m.setAuthentication("tankswap", "502970141");
		m.addTo(user.getMail(), user.getName());
		m.setFrom("tankswap@163.com", "tankswap");
		m.setSubject("密码找回，请验证");
		m.setCharset("UTF-8");
		
		String validuri="http://localhost:8080/valid?mv.id="+mv.getId()+"&mv.action="+mv.getUuid();//验证操作入口
		
		m.setHtmlMsg("<html> <head> <title>  </title>  </head> <body> " +
				"<a href='"+validuri+"'>"+validuri+"</a> " +
				"</body> </html>");
		m.setTextMsg("not support html setCharset");
		m.send();
		System.out.println("sended!");

	}

}
